﻿using System;
using System.Collections.Generic;
using System.Text;
using RockCompany.Common.Lib;
using RockCompany.Master.DTO;
using RockCompany.Common.Constant;
using System.Windows.Forms;
using System.Data;

namespace RockCompany.Master.Model
{
    public class SI07AFormModel : CommonModel
    {
        #region Properties
        private DataTable _prodImportDetail;
        public DataTable prodImportDetail
        {
            get { return _prodImportDetail; }
            set { _prodImportDetail = value; }
        }
        #endregion

        #region Constructor
        public SI07AFormModel()
        {
            prodImportDetail = new DataTable();
            DataList = new List<object>();
            dbConnector = new DBConnector();
        }
        #endregion

        #region Utility Methods
        public DataRow getCheckedRow(int index)
        {
            DataRow curRow = prodImportDetail.Rows[index];
            curRow.SetField<bool>(PHIEU_NK_TP_CT_DTO.N_CHON, !curRow.Field<bool>(PHIEU_NK_TP_CT_DTO.N_CHON));
            return curRow;
        }

        private DataTable searchWorkOrderDetail(List<string> sWorkOrderList)
        {
            DataTable tempDB = null;
            if (sWorkOrderList.Count > 0)
            {
                string sSql = "SELECT A." + CONG_LENH_SX.N_SO_LO + ", ";
                sSql += CONG_LENH_SX.N_MA_MAU + ", ";
                sSql += "SUM(" + CONG_LENH_SX_CT.N_TONG_LUONG_SX + ") AS " + CONG_LENH_SX_CT.N_TONG_LUONG_SX;
                sSql += " FROM " + CONG_LENH_SX.N_TABLE + " A," + CONG_LENH_SX_CT.N_TABLE + " B";
                sSql += " WHERE A." + CONG_LENH_SX.N_SO_LO + " = " + "B." + CONG_LENH_SX.N_SO_LO;
                sSql += " AND A." + CONG_LENH_SX.N_SO_LO + " IN (";
                foreach (string s in sWorkOrderList)
                {
                    sSql += "'" + s + "',";
                }
                sSql = sSql.Substring(0, sSql.Length - 1) + ")";
                sSql += " GROUP BY A." + CONG_LENH_SX.N_SO_LO + "," + CONG_LENH_SX.N_MA_MAU;
                tempDB = selectData(sSql);
            }
            return tempDB;
        }

        public bool addNewMatOrderDetail(List<string> sWorkOrderList)
        {
            DataTable tempDB = searchWorkOrderDetail(sWorkOrderList);
            if (tempDB != null && tempDB.Rows.Count > 0)
            {
                foreach (DataRow curRow in tempDB.Rows)
                {
                    DataRow newRow = prodImportDetail.NewRow();
                    newRow.SetField<bool>(PHIEU_NK_TP_CT_DTO.N_CHON, false);
                    newRow.SetField<string>(PHIEU_NK_TP_CT_DTO.N_SO_LO, curRow.Field<string>(CONG_LENH_SX.N_SO_LO));
                    newRow.SetField<string>(PHIEU_NK_TP_CT_DTO.N_MA_MAU, curRow.Field<string>(CONG_LENH_SX.N_MA_MAU));
                    newRow.SetField<decimal>(PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP, Util.parseDecimal(curRow[CONG_LENH_SX_CT.N_TONG_LUONG_SX]));
                    newRow.SetField<string>(PHIEU_NK_TP_CT_DTO.N_GHI_CHU, "");
                    prodImportDetail.Rows.Add(newRow);
                }
            }
            return tempDB != null && tempDB.Rows.Count > 0;
        }
        #endregion

        #region Main Methods
        private bool insertNewProdImport(PHIEU_NK_TP_CT_DTO newProdImport, DataRow newRow)
        {
            Dictionary<string, object> pv = new Dictionary<string, object>();
            pv.Add(PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK, newProdImport.MS_PHIEU_NK);
            pv.Add(PHIEU_NK_TP_CT_DTO.N_SO_LO, newRow.Field<string>(PHIEU_NK_TP_CT_DTO.N_SO_LO));
            pv.Add(PHIEU_NK_TP_CT_DTO.N_DON_VI, Util.parseString(newRow.Field<string>(PHIEU_NK_TP_CT_DTO.N_DON_VI)));
            pv.Add(PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP, newRow.Field<decimal>(PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP));
            pv.Add(PHIEU_NK_TP_CT_DTO.N_GHI_CHU, newRow.Field<string>(PHIEU_NK_TP_CT_DTO.N_GHI_CHU));
            pv.Add(PHIEU_NK_TP_CT_DTO.N_PHE_DUYET, newProdImport.PHE_DUYET);

            pv.Add(PHIEU_NK_TP_CT_DTO.N_CREATION_PROGRAM_ID, "SI07A");
            pv.Add(PHIEU_NK_TP_CT_DTO.N_CREATION_TIME, DateTime.Now);
            pv.Add(PHIEU_NK_TP_CT_DTO.N_CREATION_USER, newProdImport.CREATION_USER);
            return insertData(PHIEU_NK_TP_CT_DTO.N_TABLE, pv);
        }

        public bool updateProdImport(PHIEU_NK_TP_CT_DTO curProdImport)
        {
            string sClause = CommonConstantIF.BLANK;
            sClause += PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK + " = '" + curProdImport.MS_PHIEU_NK + "'";
            Dictionary<string, object> updateValues = new Dictionary<string, object>();
            updateValues.Add(PHIEU_NK_TP_CT_DTO.N_PHE_DUYET, curProdImport.PHE_DUYET);
            updateValues.Add(PHIEU_XK_TP_CT_DTO.N_LAST_UPDATE_TIME, DateTime.Now);
            updateValues.Add(PHIEU_XK_TP_CT_DTO.N_LAST_UPDATE_PROGRAM_ID, "SI07A");
            updateValues.Add(PHIEU_XK_TP_CT_DTO.N_LAST_UPDATE_USER, curProdImport.LAST_UPDATE_USER);
            return updateData2(PHIEU_NK_TP_CT_DTO.N_TABLE, sClause, updateValues);
        }

        private bool updateProdImport(PHIEU_NK_TP_CT_DTO curProdImport, DataRow modRow)
        {
            string sClause = CommonConstantIF.BLANK;
            sClause += PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK + " = '" + curProdImport.MS_PHIEU_NK + "'";
            sClause += " AND " + PHIEU_NK_TP_CT_DTO.N_SO_LO + " = '" + modRow.Field<string>(PHIEU_NK_TP_CT_DTO.N_SO_LO) + "'";

            Dictionary<string, object> updateValues = new Dictionary<string, object>();
            updateValues.Add(PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP, modRow.Field<decimal>(PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP));
            updateValues.Add(PHIEU_NK_TP_CT_DTO.N_DON_VI, modRow.Field<string>(PHIEU_NK_TP_CT_DTO.N_DON_VI));
            updateValues.Add(PHIEU_NK_TP_CT_DTO.N_PHE_DUYET, curProdImport.PHE_DUYET);
            updateValues.Add(PHIEU_NK_TP_CT_DTO.N_GHI_CHU, modRow.Field<string>(PHIEU_NK_TP_CT_DTO.N_GHI_CHU));
            updateValues.Add(PHIEU_XK_TP_CT_DTO.N_LAST_UPDATE_TIME, DateTime.Now);
            updateValues.Add(PHIEU_XK_TP_CT_DTO.N_LAST_UPDATE_PROGRAM_ID, "SI07A");
            updateValues.Add(PHIEU_XK_TP_CT_DTO.N_LAST_UPDATE_USER, curProdImport.LAST_UPDATE_USER);
            return updateData2(PHIEU_NK_TP_CT_DTO.N_TABLE, sClause, updateValues);
        }

        public bool saveData(PHIEU_NK_TP_CT_DTO curProdImport)
        {
            DataTable tempDb = null;
            int insertRes = 0, updateRes = 0;

            tempDb = prodImportDetail.GetChanges(DataRowState.Modified);
            int updatedRowCount = (tempDb != null ? tempDb.Rows.Count : 0);

            tempDb = prodImportDetail.GetChanges(DataRowState.Added);
            int addedRowCount = (tempDb != null ? tempDb.Rows.Count : 0);

            tempDb = prodImportDetail.GetChanges();

            if (tempDb != null)
            {
                foreach (DataRow curRow in tempDb.Rows)
                {
                    switch (curRow.RowState)
                    {
                        case DataRowState.Added:
                            if (insertNewProdImport(curProdImport, curRow))
                            {
                                insertRes++;
                            }
                            break;
                        case DataRowState.Modified:
                            if (updateProdImport(curProdImport, curRow))
                            {
                                updateRes++;
                            }
                            break;
                    }
                }
                prodImportDetail.AcceptChanges();
            }
            return insertRes == addedRowCount && updateRes == updatedRowCount;
        }
        #endregion
    }
}
